﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_KhachHangDaiChung_KhachHangDaiChung_CapNhatThongTinCaNhan : System.Web.UI.Page
{
    string CMND = "";//lay tu trong session
    bool state;
    protected void Page_Load(object sender, EventArgs e)
    {
        //coi lai bao mat cho nay///////////
        if (Request["CMND"] == null)
            Response.Redirect("~/Default.aspx");
        CMND = Request["CMND"];

        ////////////////////////////
        DataDSTableAdapters.TAIKHOANTableAdapter tkadapter = new DataDSTableAdapters.TAIKHOANTableAdapter();
        DataDS.TAIKHOANDataTable tktable = tkadapter.TAIKHOAN_LayTheoCMND(CMND);
        state = tktable[0].TrangThai;
        if (!this.IsPostBack)
        {
            chkDoiMatKhau.Checked = false;
            txtPasswordmoi.Enabled = false;
            txtPasswordmoinhaplai.Enabled = false;
            txtPasswordCu.Enabled = false;
            rqfvPasswordCu.Enabled = false;
            rqfvPasswordMoi.Enabled = false;
            cvPasswordNhapLai.Enabled = false;
            //txtPasswordmoi.Text = "huutho";
            
            txtHoTen.Text = (string)tktable.Rows[0]["HoTen"];
            txtEmail.Text = (string)tktable.Rows[0]["Email"];
            txtDiaChi.Text = (string)tktable.Rows[0]["DiaChi"];
            txtDienThoai.Text = tktable.Rows[0]["DienThoai"].ToString();
            if (tktable.Rows[0]["MaTaiKhoan_NganHang"] != null)
                txtMaTaiKhoan.Text = (string)tktable.Rows[0]["MaTaiKhoan_NganHang"].ToString();
            imgHinhAnh.ImageUrl = (string)tktable.Rows[0]["Avatar"];
            chkGuiMail.Checked = (bool)tktable.Rows[0]["Is_Mail_SP_YeuThich"];
        }

    }
    protected void chkDoiMatKhau_CheckedChanged(object sender, EventArgs e)
    {
        if (chkDoiMatKhau.Checked)
        {
            txtPasswordmoi.Enabled = true;
            txtPasswordmoinhaplai.Enabled = true;
            txtPasswordCu.Enabled = true;
            rqfvPasswordCu.Enabled = true;
            rqfvPasswordMoi.Enabled = true;
            cvPasswordNhapLai.Enabled = true;
        }
        else
        {
            txtPasswordmoi.Enabled = false;
            txtPasswordmoinhaplai.Enabled = false;
            txtPasswordCu.Enabled = false;
            rqfvPasswordCu.Enabled = false;
            rqfvPasswordMoi.Enabled = false;
            cvPasswordNhapLai.Enabled = false;
        }
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        string hinhAnh = imgHinhAnh.ImageUrl;
        string imageFolder = ConfigurationSettings.AppSettings["IMAGE_FOLDER"];
        if (fulHinhAnh.FileName != "")
            hinhAnh = imageFolder + fulHinhAnh.FileName;

        DataDSTableAdapters.TAIKHOANTableAdapter tkadapter = new DataDSTableAdapters.TAIKHOANTableAdapter();
        string matk = txtMaTaiKhoan.Text;
       
        if (chkDoiMatKhau.Checked)
        {
            string user_oldPass = txtPasswordCu.Text;
            string passwordmoi = txtPasswordmoi.Text;
            string passwordmoinhaplai = txtPasswordmoinhaplai.Text;


            DataDS.TAIKHOANDataTable tktable = tkadapter.TAIKHOAN_LayTheoCMND(CMND);
            string DB_HashPass = (string)tktable.Rows[0]["Hash_Pass"];
            string DB_Salt = (string)tktable.Rows[0]["Salt"];
            if (GlobalFunctions.verifyPassword(user_oldPass, DB_HashPass, DB_Salt) == false)
            {
                //sai mat khau cu, bao loi
                return;
            }
            if (passwordmoi != passwordmoinhaplai)
            {
                // password nhap lai khong khop, bao loi
                return;
            }
            string Salt = DateTime.Now.ToString();
            string chuoigiatrihash = GlobalFunctions.createHash(passwordmoi, Salt);



            tkadapter.TAIKHOAN_CapNhatThongTinCoPass(CMND, chuoigiatrihash, Salt, txtEmail.Text,
                txtHoTen.Text, txtDiaChi.Text, txtDienThoai.Text, hinhAnh, chkGuiMail.Checked, state);
        }
        else
        {
            tkadapter.TAIKHOAN_CapNhatThongTinThongThuong(CMND, txtEmail.Text,
               txtHoTen.Text, txtDiaChi.Text, txtDienThoai.Text, hinhAnh, chkGuiMail.Checked, state);
        }
        Response.Write("<script>alert('Cập nhật thành công!')</script>");
        Response.Redirect("KhachHangThanThiet_XemThongTinCaNhan.aspx?CMND=" + CMND);
    }
}
